package leetcode.medium.limit100;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author yaoyao1.liu@zhaopin.com.cn
 * @since 2022-11-11 14:26
 **/
public class No78 {
    public static void main(String[] args) {
        No78 no78 = new No78();
        List<List<Integer>> subsets = no78.subsets(new int[]{1, 2, 3});
        System.out.println(subsets);

    }

    public List<List<Integer>> subsets(int[] nums) {
        int n = nums.length;
        List<List<Integer>> subsets = new ArrayList<>();
        List<Integer> t = new ArrayList<>();
        for (int mask = 0; mask < (1 << n); ++mask) {
            t.clear();
            for (int i = 0; i < n; ++i) {
                if ((mask & (1 << i)) != 0) {
                    t.add(nums[i]);
                }
            }
            subsets.add(new ArrayList<Integer>(t));
        }
        return subsets;
    }

    public void deal(List<List<Integer>> list, List<Integer> one, int[] nums, int length, int index) {
        if (index < length && !check(list, one)) {
            list.add(new ArrayList<>(one));
        }
        for (int i = index; i < nums.length; i++) {
            one.add(nums[i]);
            deal(list, one, nums, nums.length, index + 1);
            one.remove(one.size() - 1);
        }
    }

    public boolean check(List<List<Integer>> list, List<Integer> one) {
        one.sort(Integer::compare);
        for (List<Integer> integers : list) {
            integers.sort(Integer::compare);
            if (integers.size() != one.size()) {
                continue;
            }
            boolean flag = true;
            for (int i = 0; i < integers.size(); i++) {
                flag = flag && Objects.equals(integers.get(i), one.get(i));
            }
            if (flag) {
                return true;
            }
        }
        return false;
    }

}
